;export default class Page<T> {

    method:string;
    data_source:Array<T>;
    dataFilter:Function;
    action:string;
    page_size:Number;
    current_page:Number;
    totalCount:Number;
    order_by:any;
    items:Array<T>;
    action_data: any;  
    callback:Function;
    errorCallback:Function;
    beforeGetData:Array<Function>;
    afterGetData:Array<Function>;
    loadingStatus:String = 'success';

    constructor(action:string, action_data?:any, callback?:Function, errorCallback?:Function) {
        this.method = 'get';
        if(typeof(action) == 'string') {
            this.action = action;
        }else {
            this.data_source = [];
            this.dataFilter = (source, params) => {
                var begin = (params.current_page - 1) * params.page_size;
                var end = Math.min(begin + params.page_size, source.length);
                return {
                    totalCount: source.length,
                    items: source.slice(begin, end)
                }
            };
        }
        this.page_size = 10;
        this.current_page = 1;
        this.totalCount = 0;
        this.order_by = {};
        this.items = [];
        this.action_data = action_data || {};  
        this.callback = callback || function(){}
        this.errorCallback = errorCallback || function(){}
        this.beforeGetData = [];
        this.afterGetData = [];
    }

    actionData(action_data) {
        if(action_data) {
            this.action_data = action_data;
            return this;
        }else {
            return this.action_data;
        }
    }

    dataSource(source) {
        if(source) {
            this.data_source = source;
            this.current_page = 1;
            return this;
        }else {
            return this.data_source;
        }
    }

    pageSize(page_size) {
        if(page_size) {
            this.page_size = page_size;
            return this;
        }else {
            return this.page_size;
        }
    }

    // 将对象序列化为一个json对象，可以用于http请求或对象复制
    serialize() {
        return Object.assign({
            current_page: this.current_page,
            page_size: this.page_size,
            order_by: this.order_by
        }, this.action_data);
    }

    // 用json数据为对象属性赋值
    parse(data:any) {
        this.totalCount = data.totalCount;
        this.items = data.items;
    }

    getData(callback?:Function, errorCallback?:Function) {
        this.loadingStatus = 'loading';
        this.items = [];
        callback = callback || this.callback;
        errorCallback = errorCallback || this.errorCallback;
        this.beforeGetData.forEach(item => item());


        var forSuccess = result => {
            this.parse(result);
            callback.call(this, result);
            this.loadingStatus = this.totalCount ? 'success' : 'nodata';
            this.afterGetData.forEach(item => item(result));
        };
        var forError = error => {
            errorCallback.call(this, error);
            this.afterGetData.forEach(item => item(null));
            this.loadingStatus = 'nodata';
        }

        if(!this.action) {
            forSuccess(this.dataFilter(this.data_source, this.serialize()));
        }else {
            Page.handleData(this.action, this.serialize(), forSuccess, forError);
        }
    }

    exportExcel() {
        //Vue.prototype.api.getResource(this.action, Object.assign(this.serialize(), { excel: true }));
    }

    static handleData(action:string, data: any, callback:Function, errCallback:Function) {
        callback(null);
    }
};